﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuMianChaZhi.SurfaceInterpolation
{
    class InterpolationMethodThree : InterpolationMethod
    {
       
        public double[,] CalculateInterpolation(List<Point> initPoints)
        {

            var preLists = preProcess(initPoints);

            InterpolationMethodOne one = new InterpolationMethodOne();
            double[,] ret = one.CalculateInterpolation(preLists);

            return ret;          
        }


        private List<Point> preProcess(List<Point> initPoint)
        {
            List<Point> rets = new List<Point>();


            var temps = xianXingChaZhiFourPoint(initPoint[0], initPoint[1]);
            rets.AddRange(temps);

            temps = xianXingChaZhiFourPoint(initPoint[1], initPoint[2]);
            rets.AddRange(temps);

            temps = xianXingChaZhiFourPoint(initPoint[2], initPoint[3]);
            rets.AddRange(temps);

            temps = xianXingChaZhiFourPoint(initPoint[3], initPoint[0]);

            rets.AddRange(temps);

            rets.AddRange(initPoint);
            return rets;


            List<Point> lastResult = new List<Point>();
            foreach (var pt in rets)
            {
                temps = chazhiFromZero(initPoint[4],pt);
                lastResult.AddRange(temps);
            }

            lastResult.AddRange(initPoint);

            return lastResult;
        }


        private double getDistance(Point p1, Point p2)
        {
            double d = Math.Sqrt(Math.Pow((p2.x - p1.x), 2) + Math.Pow((p2.y - p1.y), 2));

            return d;
        }

        private List<Point> xianXingChaZhiFourPoint(Point p1,Point p2)
        {
            List<Point> rets = new List<Point>();


            double xOffSet = (p2.x - p1.x) / 5;

            double dbase = getDistance(p2, p1);

            for(int x = 1; x < 5; x++)
            {
                double xx = p1.x + xOffSet * x;

                double yy = p1.y + ((p2.y - p1.y) / (p2.x - p1.x)) * (xx - p1.x);

                Point pt = new Point();
                pt.x = xx;
                pt.y = yy;

                double dtemp = getDistance(pt, p1);

                pt.z = p1.z + ((p2.z - p1.z)/dbase) *dtemp;

                rets.Add(pt);
            }

            return rets;
        }

        private List<Point> chazhiFromZero(Point p0, Point p1)
        {
            List<Point> rets = new List<Point>();

            double xOffSet = (p1.x - p0.x) / 5;

            double dbase = getDistance(p1, p0);

            int x = 0;
            do
            {
                x++;
                //第五个跳过
                if (x == 5)
                    continue;

                double xx = p0.x + xOffSet * x;

                double yy = p0.y + ((p1.y - p0.y) / (p1.x - p0.x)) * (xx - p0.x);

                Point pt = new Point();
                pt.x = xx;
                pt.y = yy;

                double dtemp = getDistance(pt, p0);

                if (dtemp > 50)
                    break;


                pt.z = p0.z + ((p1.z - p0.z) / dbase) * dtemp;

                rets.Add(pt);
            }
            while (true);

            return rets;

        }


    }
}
